package 二叉树相关问题.中度题;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class 删点成林 {
    List<TreeNode> list = new ArrayList<>();
    Set<Integer> set = new HashSet<>();
    public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
        for (int i : to_delete) {
            set.add(i);
        }
        if (!set.contains(root.val))
            list.add(root);
        bfs(root);
        return list;
    }

    private TreeNode bfs(TreeNode root) {
        if (root==null)
            return null;
        root.left=bfs(root.left);
        root.right=bfs(root.right);
        if (set.contains(root.val)){
            if (root.left!=null)
                list.add(root.left);
            if (root.right!=null)
                list.add(root.right);
            root=null;
        }
        return root;
    }
}
